﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Configuration;

namespace MyBusiness
{
    /// <summary>
    /// 数据转换为json对象
    /// </summary>
    public class DataToJson
    {

        #region Json过滤特殊字符
        /// <summary>
        /// Json过滤特殊字符
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="columnValue"></param>
        /// <returns></returns>
        private string JsonFilter(string columnName, string columnValue)
        {
            string strJson = "\"" + columnName.ToLower() + "\":\"" + columnValue.Replace("\r", " ").Replace("\n", "").
                Replace("\\", "\\\\").Replace(" 0:00:00", "").Replace("\"", "'") + "\",";

            return strJson;
        }
        #endregion

        #region 将datatable中的数据转换成json格式
        /// <summary>
        /// 将datatable中的数据转换成json格式
        /// </summary>
        /// <param name="dTable"></param>
        /// <returns></returns>
        public string BindJson(DataTable dTable)
        {
            StringBuilder sbJson = new StringBuilder("[");
            StringBuilder drJson = new StringBuilder();
            if (dTable != null && dTable.Rows.Count > 0)
            {
                foreach (DataRow dr in dTable.Rows)
                {
                    drJson.Append("{");
                    foreach (DataColumn dc in dTable.Columns)
                    {
                        drJson.Append(JsonFilter(dc.ColumnName, dr[dc.ColumnName].ToString()));
                    }
                    if (dTable.Columns.Count >= 1)
                    {
                        drJson = drJson.Remove(drJson.Length - 1, 1);
                    }
                    drJson.Append("},");
                }
                if (dTable.Rows.Count > 0)
                    drJson = drJson.Remove(drJson.Length - 1, 1);
            }
            sbJson.Append(drJson.ToString() + "]");
            return sbJson.ToString();
        }
        #endregion

        #region 将datatable中的数据转换成json格式带table表名
        /// <summary>
        /// 将datatable中的数据转换成json格式带table表名
        /// </summary>
        /// <param name="dTable"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public string BindJson(DataTable dTable, string tableName)
        {
            StringBuilder sbJson = new StringBuilder("{table:'" + tableName.ToLower() + "',row:[");
            StringBuilder drJson = new StringBuilder();
            foreach (DataRow dr in dTable.Rows)
            {
                drJson.Append("{");
                foreach (DataColumn dc in dTable.Columns)
                {
                    drJson.Append(JsonFilter(dc.ColumnName, dr[dc.ColumnName].ToString()));
                }
                drJson.Append("},");
            }
            if (dTable.Rows.Count > 0)
                drJson = drJson.Remove(drJson.Length - 1, 1);
            sbJson.Append(drJson.ToString() + "]}");

            return sbJson.ToString();
        }
        #endregion

        #region 递归构建JSON树
        /// <summary>
        /// 必须包含id,pid,text三个字段
        /// </summary>
        /// <param name="dTable"></param>
        /// <returns></returns>
        public string BindJsonTree(DataTable dTable)
        {
            StringBuilder sbJson = new StringBuilder("");
            BindJsonTree(dTable, "0", "0", "id", "pid", ref sbJson);
            return sbJson.ToString();
        }

        /// <summary>
        /// 递归构建树
        /// </summary>
        /// <param name="dTable"></param>
        /// <param name="id"></param>
        /// <param name="beginValue"></param>
        /// <param name="drJson"></param>
        public void BindJsonTree(DataTable dTable, string id, string beginValue, string sonField, string parentField, ref StringBuilder drJson)
        {
            DataRow[] drs = dTable.Select(parentField + "='" + id + "'");
            if (drs.Length >= 1)
            {
                drJson.Append(id != beginValue ? ",isexpand:false,children:[" : "[");
                foreach (DataRow dr in drs)
                {
                    drJson.Append("{");
                    string strAttr = "\"attributes\":{";
                    foreach (DataColumn dc in dTable.Columns)
                    {
                        string _json = JsonFilter(dc.ColumnName, dr[dc.ColumnName].ToString());
                        drJson.Append(_json);
                        strAttr += _json;
                    }
                    if (dTable.Columns.Count > 0)
                    {
                        strAttr = strAttr.Remove(strAttr.Length - 1, 1) + "},";
                    }
                    drJson.Append(strAttr);
                    if (dTable.Columns.Count >= 1)
                    {
                        drJson = drJson.Remove(drJson.Length - 1, 1);
                    }
                    if (sonField != "")
                    {
                        BindJsonTree(dTable, dr[sonField].ToString(), beginValue, sonField, parentField, ref drJson);
                    }
                    drJson.Append("},");
                }
                if (drs.Length >= 1)
                    drJson = drJson.Remove(drJson.Length - 1, 1);
                drJson.Append("]");
            }
        }

        /// <summary>
        /// 构建树 单数 不带子节点
        /// </summary>
        /// <param name="dTable"></param>
        /// <param name="state"></param>
        /// <param name="drJson"></param>
        public string BindJsonTreeSinge(DataTable dTable)
        {
            StringBuilder drJson = new StringBuilder();
            drJson.Append("[");
            for (int i = 0; i < dTable.Rows.Count; i++)
            {
                drJson.Append("{");
                foreach (DataColumn dc in dTable.Columns)
                {
                    drJson.Append("\"" + dc.ColumnName.ToLower() + "\":\"" + dTable.Rows[i][dc.ColumnName].ToString().Replace("\r", " ").Replace("\n", "").Replace("\\", "\\\\") + "\",");
                }
                drJson = drJson.Remove(drJson.Length - 2, 2);
                drJson.Append("\"},");
            }
            if (dTable.Rows.Count >= 1)
                drJson = drJson.Remove(drJson.Length - 1, 1);
            drJson.Append("]");
            return drJson.ToString();
        }

        /// <summary>
        /// 必须包含id,pid,text三个字段
        /// </summary>
        /// <param name="dTable"></param>
        /// <returns></returns>
        public string BindJsonTreeExt(DataTable dTable, string state)
        {
            StringBuilder sbJson = new StringBuilder("");
            BindJsonTreeExt(dTable, state, ref sbJson);
            return sbJson.ToString();
        }

        /// <summary>
        /// 递归构建树
        /// </summary>
        /// <param name="dTable"></param>
        /// <param name="id"></param>
        /// <param name="beginValue"></param>
        /// <param name="drJson"></param>
        public void BindJsonTreeExt(DataTable dTable, string state, ref StringBuilder drJson)
        {
            drJson.Append("[");
            for (int i = 0; i < dTable.Rows.Count; i++)
            {
                drJson.Append("{");
                string strAttr = "\"attributes\":{";
                foreach (DataColumn dc in dTable.Columns)
                {
                    drJson.Append("\"" + dc.ColumnName.ToLower() + "\":\"" + dTable.Rows[i][dc.ColumnName].ToString().Replace("\r", " ").Replace("\n", "").Replace("\\", "\\\\") + "\",");
                    strAttr += "\"" + dc.ColumnName.ToLower() + "\":\"" + dTable.Rows[i][dc.ColumnName].ToString().Replace("\r", " ").Replace("\n", "").Replace("\\", "\\\\") + "\",";
                }
                if (dTable.Columns.Count > 0)
                {
                    strAttr = strAttr.Remove(strAttr.Length - 1, 1) + "},";
                }
                drJson.Append(strAttr);
                drJson.Append("\"state\":\"" + state + "\"},");
            }
            if (dTable.Rows.Count >= 1)
                drJson = drJson.Remove(drJson.Length - 1, 1);
            drJson.Append("]");

        }
        #endregion

       
    }
}
